package com.link.base.core.kafka.consumer;

import com.alibaba.fastjson.JSONObject;
import com.link.base.base.common.service.SendSmsService;
import com.link.base.base.interstatistics.util.VisitStatisticsConstants;
import com.link.core.cllog.LCLogger;
import com.link.core.kafka.KafkaConnectCondition;
import com.link.core.util.RedisUtil;
import com.link.core.util.StringUtils;
import com.link.core.util.redisclient.LinkRedisClient;
import org.springframework.context.annotation.Conditional;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * business工程kafka消费者
 *
 * @author LiYang
 * @date 20190807
 */
@Component
@Conditional(KafkaConnectCondition.class)
public class Reciever {

    @Resource
    private SendSmsService sendSmsService;

    /**
     * 迁移的原发送短信和email的代码
     *
     * @author LiYang
     * @date 20190807
     * @param msgStr
     */
    @KafkaListener(topics = {"SEND_RESTRICTION_MAIL"})
    public void sendRestrictionMailReciever(String msgStr) {
        LinkRedisClient jedis = RedisUtil.getJedis();
        try {
            if (!StringUtils.isBlank(msgStr)) {
                String phones = jedis.hget(VisitStatisticsConstants.API_STATISTICS_CONFIG_KEY, VisitStatisticsConstants.API_CONFIG_MSG_PHONES);
                List<String> phoneList = JSONObject.parseArray("[" + phones + "]", String.class);
                //模板id
                String msgCode = jedis.hget(VisitStatisticsConstants.API_STATISTICS_CONFIG_KEY, VisitStatisticsConstants.API_CONFIG_MSG_EXAMPLE_CODE);

                //发送短信
                sendSmsService.tencentMultiSendSms(msgCode, msgStr, null, phoneList);
            }
        } catch (Exception e) {
            LCLogger.error().withMessageKey("sendRestrictionMailReciever").withMessage("发送超频短信和邮件出现异常：" + e.getMessage()).flush();
        } finally {
            RedisUtil.returnResource(jedis);
        }
    }
}
